package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.TeachStatisticsAddReq;
import com.zucc.controller.req.TeachStatisticsModifyReq;
import com.zucc.controller.req.TeachStatisticsSearchReq;
import com.zucc.controller.resp.TeachStatisticsResp;
import com.zucc.entity.TeachStatistics;
import com.zucc.entity.TeachStatisticsMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.TeachStatisticsService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-11 8:56 下午
 */
@Service
public class TeachStatisticsServiceImpl extends MyServiceImpl<TeachStatistics> implements TeachStatisticsService {


    @Resource
    private TeachStatisticsMapper teachStatisticsMapper;

    @Override
    public MyMapper<TeachStatistics> mapper() {
        return teachStatisticsMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(TeachStatisticsAddReq req) {
        TeachStatistics teachStatistics = new TeachStatistics();
        BeanUtil.copyProperties(req, teachStatistics);
        teachStatistics.setDelFlag(0);
        teachStatistics.setCreateTime(DateUtil.date());
        teachStatistics.setModifyTime(DateUtil.date());
        teachStatistics.setApprovalStatus(0);
        super.saveKey(teachStatistics);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(TeachStatisticsModifyReq req) {
        TeachStatistics teachStatisticsExist = super.findById(req.getId());
        if (Objects.isNull(teachStatisticsExist) || teachStatisticsExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的教学情况统计信息");
        }
        BeanUtil.copyProperties(req, teachStatisticsExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        teachStatisticsExist.setModifyTime(DateUtil.date());
        super.updateSelective(teachStatisticsExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        TeachStatistics teachStatisticsExist = super.findById(id);
        if (Objects.isNull(teachStatisticsExist) || teachStatisticsExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的教学情况统计信息");
        }
        teachStatisticsExist.setDelFlag(1);
        teachStatisticsExist.setDeleteTime(DateUtil.date());
        super.updateSelective(teachStatisticsExist);
    }

    @Override
    public PageData<TeachStatisticsResp> page(TeachStatisticsSearchReq req) {
        Example example = new Example(TeachStatistics.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);
        if (Objects.nonNull(req.getApprovalStatus())) {
            criteria.andEqualTo("approvalStatus", req.getApprovalStatus());
        }
        if (StrUtil.isNotBlank(req.getRemarks())) {
            criteria.andLike("remarks", "%" + req.getRemarks() + "%");
        }
        if (StrUtil.isNotBlank(req.getWeek())) {
            criteria.andLike("week", "%" + req.getWeek() + "%");
        }
        if (StrUtil.isNotBlank(req.getTimeInfo())) {
            criteria.andLike("timeInfo", "%" + req.getTimeInfo() + "%");
        }
        if (StrUtil.isNotBlank(req.getTask())) {
            criteria.andLike("task", "%" + req.getTask() + "%");
        }
        if (StrUtil.isNotBlank(req.getClassCode())) {
            criteria.andLike("classCode", "%" + req.getClassCode() + "%");
        }
        if (StrUtil.isNotBlank(req.getClassIndex())) {
            criteria.andLike("classIndex", "%" + req.getClassIndex() + "%");
        }
        if (StrUtil.isNotBlank(req.getTeacher())) {
            criteria.andLike("teacher", "%" + req.getTeacher() + "%");
        }
        if (StrUtil.isNotBlank(req.getAddress())) {
            criteria.andLike("address", "%" + req.getAddress() + "%");
        }
        PageData<TeachStatistics> pageData = super.findByPage(req, example);
        PageData<TeachStatisticsResp> convert = MyPageUtil.convert(pageData, TeachStatisticsResp.class);
        return convert;
    }
}
